package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {

    @Autowired
    private IYddUserService yddUserService;

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id) {
        YddUser yddUser = yddUserService.getById(id);
        return ResultData.ok("回显成功",yddUser);
    }

    /**
     * 分页查询
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer sex, String username, String mobile, int page, int limit) {
        Page<YddUser> pageInfo = new Page<>(page,limit);
        LambdaQueryWrapper<YddUser> lqw =new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(username),YddUser::getUsername,username);
        lqw.eq(sex!=null,YddUser::getSex,sex);
        lqw.likeRight(StringUtils.isNotEmpty(mobile), YddUser::getMobile,mobile);
        yddUserService.page(pageInfo,lqw);
        Map<String, Object> map = new HashMap<>();
        map.put("data", pageInfo.getRecords());
        map.put("totalPages", pageInfo.getPages());
        map.put("currentPage", pageInfo.getCurrent());
        map.put("pageSize", pageInfo.getSize());
        map.put("count", pageInfo.getTotal());
        return ResultData.ok("成功", map);
    }

    /**
     * 添加
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddUser yddUser){
        yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
        boolean flag = yddUserService.save(yddUser);
        if (flag){
            return ResultData.ok("添加成功",flag);
        }
        return ResultData.error("添加失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Long id){
        boolean flag = yddUserService.removeById(id);
        if (flag){
            return ResultData.ok("删除成功",flag);
        }
        return ResultData.error("删除失败");
    }

    /**
     * 修改
     * @param yddUser
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddUser yddUser){
        boolean flag = yddUserService.updateById(yddUser);
        if (flag){
            return ResultData.ok("修改成功",flag);
        }
        return ResultData.error("修改失败");
    }
}
